﻿#include "WgtSqlConfig.h"
#include <QFormLayout>

#pragma execution_character_set("utf-8")

WgtSqlConfig::WgtSqlConfig(QWidget *parent)
    : QWidget{parent}
{
    setupUi();
}

void WgtSqlConfig::setConfig(SqlLinkParam param)
{
    ledtLinkName->setText(param.linkName);
    cbboxSqlType->setCurrentText(param.sqlType);
    ledtIp->setText(param.ip);
    spboxPort->setValue(param.port);
    ledtUser->setText(param.user);
    ledtPassword->setText(param.password);
    ledtDbName->setText(param.dbName);
    ledtTbName->setText(param.tableName);
}

void WgtSqlConfig::setupUi()
{
    ledtLinkName = new QLineEdit("linkName");
    cbboxSqlType = new ComboBox();
    cbboxSqlType->addItems({"QMYSQL", "QSQLITE"});
    ledtIp = new QLineEdit("127.0.0.1");
    spboxPort = new SpinBox();
    spboxPort->setRange(0, 100000);
    spboxPort->setValue(3306);
    ledtUser = new QLineEdit("root");
    ledtPassword = new QLineEdit("root");
    ledtDbName = new QLineEdit("DatabaseName");
    ledtTbName = new QLineEdit("TableName");
    pbtnSave = new QPushButton(tr("保存"));

    QFormLayout* fLayout = new QFormLayout();
    this->setLayout(fLayout);
    fLayout->addRow(tr("数据库连接名"), ledtLinkName);
    fLayout->addRow(tr("数据库类型"), cbboxSqlType);
    fLayout->addRow("IP", ledtIp);
    fLayout->addRow(tr("端口"), spboxPort);
    fLayout->addRow(tr("用户名"), ledtUser);
    fLayout->addRow(tr("密码"), ledtPassword);
    fLayout->addRow(tr("数据库名"), ledtDbName);
    fLayout->addRow(tr("表名"), ledtTbName);
    fLayout->addRow(pbtnSave);

    connect(pbtnSave, &QPushButton::clicked, this, [&](){
        SqlLinkParam param;
        param.linkName = ledtLinkName->text();
        param.sqlType = cbboxSqlType->currentText();
        param.ip = ledtIp->text();
        param.port = spboxPort->value();
        param.user = ledtUser->text();
        param.password = ledtPassword->text();
        param.dbName = ledtDbName->text();
        param.tableName = ledtTbName->text();
        emit sigConfigChanged(param);
    });

}


